//
//  Particle.m
//  GLSprite
//
//  Created by Daniel Polak on 7/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Particle.h"
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@implementation Particle

@synthesize Texture;
@synthesize LifeSpan = m_fLifeSpan;

const GLshort myTexcoords[] = {
	0, 0,
	1, 0,
	0, 1,
	1, 1,
};

-(void)Create:(float)Life :(GLuint)nTex: (CGPoint) Location: (CGPoint)Velocity
{
	[self SetTextureID: nTex];
	m_fLifeSpan = Life;
	m_pLocation = Location;
	
	//TEST VELOCITY
	CGPoint testVelocity;
	testVelocity.x = testVelocity.y = 0.01f;
	m_pVelocity = testVelocity;
	//END TEST
	
	//Vert Array
	m_spriteVertices[0] = m_pLocation.x - .05f;
	m_spriteVertices[1] = m_pLocation.y - .05f;
	m_spriteVertices[2] = m_pLocation.x + .05f;
	m_spriteVertices[3] = m_pLocation.y - .05f;
	m_spriteVertices[4] = m_pLocation.x - .05f;
	m_spriteVertices[5] = m_pLocation.y + .05f;
	m_spriteVertices[6] = m_pLocation.x + .05f;
	m_spriteVertices[7] = m_pLocation.y + .05f;
	
	
	
	//Set up the vert array for use
	glVertexPointer(2, GL_FLOAT, 0, m_spriteVertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	glTexCoordPointer(2, GL_SHORT, 0, myTexcoords);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

-(void)Update:(float)fTime
{
	m_fLifeSpan -= fTime;
	
	//Update it's position
	m_pLocation.x += m_pVelocity.x;
	m_pLocation.y += m_pVelocity.y - 9.8f*fTime;
	
	//Vert Array
	m_spriteVertices[0] = m_pLocation.x - .05f;
	m_spriteVertices[1] = m_pLocation.y - .05f;
	m_spriteVertices[2] = m_pLocation.x + .05f;
	m_spriteVertices[3] = m_pLocation.y - .05f;
	m_spriteVertices[4] = m_pLocation.x - .05f;
	m_spriteVertices[5] = m_pLocation.y + .05f;
	m_spriteVertices[6] = m_pLocation.x + .05f;
	m_spriteVertices[7] = m_pLocation.y + .05f;
	
	//Render
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

}

@end
